<?php
/**
 * User: M2
 * Date: 13-11-7
 */
class YlPageItemTaglib {
    public function parse($content,$arr=array(),$count,$pagesize,$nowPage,$domain) {
        $matches = preg_parse($content, 'yl:pageItem');
        foreach ($matches as $match) {
            $catenaming = common('Array')->getParseTemplate($arr,$arr['catenaming'],'cateid');
            if(!empty($catenaming)) {
                $arr = explode('.',$catenaming);
                $url = $domain.'/'.ltrim($arr[0], '/');
                $prex = $arr[1];
                $text = getParseOneAttr($match, 'text');
                $rollPage = getParseOneAttr($match, 'rollPage');
                //OOO fix by  jepin 2014 0905
                $rollPage = empty($rollPage) ? 9 : $rollPage;
                if ($count < $pagesize)
                    return;
                // 总页数
                $totalPages = ceil ( $count / $pagesize );
                
                //OOO  add by jepin 20140905 
                if($rollPage>$totalPages){
                	$rollPage = $totalPages;
                }
                // 分页栏的总页数
                $coolPages = ceil ( $totalPages / $rollPage );

                if (! empty ( $totalPages ) && $nowPage > $totalPages) {
                    $nowPage = $totalPages;
                }
                // 起始页
                $firstRow = $pagesize * ($nowPage - 1);
                // 当前分页栏页数
                $nowCoolPage = ceil ( $nowPage / $rollPage );
                $parameter = '';
                // 向上翻
                $upRow = $nowPage - 1;
                // 向下翻
                $downRow = $nowPage + 1;
                $parseStr = '';
                $type = getParseOneAttr($match, 'type');
                if (isset ( $type ) && ! empty ( $type )) {
                    // 总数
                    if (strtolower ( $type ) == 'totalnum') {
                        $parseStr = '共' . $count . '条数据&nbsp;';
                    } elseif (strtolower ( $type ) == 'firstpage') {
                        $text = empty($text)?'首页':$text;
                        if ($nowPage == 1) {
                           // $parseStr = $text.'&nbsp;';
                            $parseStr ='';
                        } else {
                            $parseStr = "<span class=\"first\"><a href='".$url.'.'.$prex."'>".$text."</a>&nbsp;</span>";
                        }
//                         if ($nowCoolPage != 1) {
//                             $preRow = $nowPage - $rollPage;
//                             if($preRow == 1)
//                                 $parseStr = "<a href='".$url.'.'.$prex."'>上".$rollPage."页</a>";
//                             else
//                                 $parseStr = "<a href='".$url."_".$preRow.'.'.$prex."'>上".$rollPage."页</a>";
//                         }
                    } elseif (strtolower ( $type ) == 'previouspage') {
                        $text = empty($text)?'上一页':$text;
                        if ($upRow > 0) {
                            if($upRow == 1)
                               // $parseStr = "<a href='".$url.'.'.$prex."'>".$text."</a>";
                                $parseStr ='';
                            else{
                            	$parseStr = "<span class=\"prev\"><a href='".$url."_".$upRow.'.'.$prex."'>".$text."</a></span>";
                            }
                        } else {
                           // $parseStr = $text;
                        }
                    } elseif (strtolower ( $type ) == 'pagenavigation') { // 显示数字
                        for($i = 1; $i <= $rollPage; $i ++) {
                        	
                        //add by jepin 20140905  分页逻辑修改	
                        	$str = getParseContentText($match, 'yl:pageItem');
                        	
                        	if(($nowPage-5)<0){  //  当前页小于5页时
                        	    $page = $i;
                        		if($i==$nowPage){ 
                        			if($i==1){
                        			     $pageurl = '';
                                         $pagenum = $page;
                                         $matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
                                         $matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
                                         $str = taglib(C('yl:failuretemplate'))->parse($matchFailureTemplate[0]);
                                         $str = str_replace($matchSuccessTemplate[0], '', $str);
                                         if(strpos($str,'{') && strpos($str,'}')) {
                                             $parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
                                         }
                        			}else{
                        				//$pageurl = $url.'_'.$page.'.'.$prex;
                        				$pageurl = '';
                        				$pagenum = $page;
                        				$matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
                        				$matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
                        				$str = taglib(C('yl:failuretemplate'))->parse($matchFailureTemplate[0]);
                        				$str = str_replace($matchSuccessTemplate[0], '', $str);
                        				if(strpos($str,'{') && strpos($str,'}')) {
                        					$parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
                        				}
                        			}
                        		}else{
                        			if(1==$page){
                        				$pageurl = $url.'.'.$prex;
                        			}else{
                        				$pageurl = $url.'_'.$page.'.'.$prex;
                        			}
                        			$pagenum = $page;
                        			$matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
                        			$matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
                        			$str = taglib(C('yl:successtemplate'))->parse($matchSuccessTemplate[0]);
                        			$str = str_replace($matchFailureTemplate[0], '', $str);
                        			if(strpos($str,'{') && strpos($str,'}')) {
                        			    $parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
                        			}
                        			
                        		}
                        	}elseif(($nowPage+5)>$totalPages){//  当前页处在末尾5页时
                        		if($nowPage<=$rollPage){
                        			$page =$i;
                        		}else{
                        		    $page = $totalPages+$i-9;
                        		}
                        		if($page==$nowPage){
                        			    if(1==$page){
                        			    	$pageurl = $url.'.'.$prex;
                        			    }else{
                        			    	$pageurl = $url.'_'.$page.'.'.$prex;
                        			    }
                        				$pagenum = $page;
                        				$matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
                        				$matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
                        				$str = taglib(C('yl:failuretemplate'))->parse($matchFailureTemplate[0]);
                        				$str = str_replace($matchSuccessTemplate[0], '', $str);
                        				if(strpos($str,'{') && strpos($str,'}')) {
                        					$parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
                        				}
                        		}else{
                        			    $pageurl = $url.'_'.$page.'.'.$prex;
                        			    $pagenum = $page;
                        			    $matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
                        			    $matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
                        			    $str = taglib(C('yl:successtemplate'))->parse($matchSuccessTemplate[0]);
                        			    $str = str_replace($matchFailureTemplate[0], '', $str);
                        			    if(strpos($str,'{') && strpos($str,'}')) {
                        			    	$parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
                        			    }
                        		}
                        	}else{// 当处在9页中间时的逻辑
                        		 $page = $nowPage + $i -5;
                        		
                        		if(1==$page){
                        			$pageurl = $url.'.'.$prex;
                        		}else{
                        			$pageurl = $url.'_'.$page.'.'.$prex;
                        		}
                        		$pagenum = $page;
                        		if($page == $nowPage){
                        			$matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
                        			$matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
                        			$str = taglib(C('yl:failuretemplate'))->parse($matchFailureTemplate[0]);
                        			$str = str_replace($matchSuccessTemplate[0], '', $str);
                        			if(strpos($str,'{') && strpos($str,'}')) {
                        				$parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
                        			}
                        		}else{
                        			$matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
                        			$matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
                        			$str = taglib(C('yl:successtemplate'))->parse($matchSuccessTemplate[0]);
                        			$str = str_replace($matchFailureTemplate[0], '', $str);
                        			if(strpos($str,'{') && strpos($str,'}')) {
                        				$parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
                        			}
                        		}
                        	}
                        	
                        	
/**                           废弃分页处理方式                                 **/
                          //  $page = ($nowCoolPage - 1) * $rollPage + $i;
//                             $str = getParseContentText($match, 'yl:pageItem');
//                             $page = ($nowCoolPage - 1) * $rollPage + $i;
//                             if ($page != $nowPage) {
//                                 if($page == 1) {
//                                     $pageurl = $url.'.'.$prex;
//                                     $pagenum = $page;
//                                     $matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
//                                     $matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
//                                     $str = taglib(C('yl:successtemplate'))->parse($matchSuccessTemplate[0]);
//                                     $str = str_replace($matchFailureTemplate[0], '', $str);
//                                     if(strpos($str,'{') && strpos($str,'}')) {
//                                         $parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
//                                     }
//                                 } elseif ($page <= $totalPages) {
//                                     $pageurl = $url.'_'.$page.'.'.$prex;
//                                     $pagenum = $page;
//                                     $matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
//                                     $matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
//                                     $str = taglib(C('yl:successtemplate'))->parse($matchSuccessTemplate[0]);
//                                     $str = str_replace($matchFailureTemplate[0], '', $str);
//                                     if(strpos($str,'{') && strpos($str,'}')) {
//                                         $parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
//                                     }
//                                 } else {
//                                     break;
//                                 }
//                             } else {
//                                 if ($totalPages != 1) {
//                                     $pageurl = '';
//                                     $pagenum = $page;
//                                     $matchSuccessTemplate = preg_parse($str, 'yl:successTemplate');
//                                     $matchFailureTemplate = preg_parse($str, 'yl:failureTemplate');
//                                     $str = taglib(C('yl:failuretemplate'))->parse($matchFailureTemplate[0]);
//                                     $str = str_replace($matchSuccessTemplate[0], '', $str);
//                                     if(strpos($str,'{') && strpos($str,'}')) {
//                                         $parseStr .= str_replace('{Current.Num}',$pagenum,str_replace('{Current.Url}',$pageurl,$str));
//                                     }
//                                 }
//                             }
                        }
                    } elseif (strtolower ( $type ) == 'nextpage') { // 下一页
                        $text = empty($text)?'下一页':$text;
                        if ($downRow <= $totalPages) {
                            $parseStr = " <span class=\"prev\"><a href='".$url."_".$downRow.'.'.$prex."'>".$text."</a>&nbsp;</span>";
                        } else {
                           // $parseStr = $text.'&nbsp;';
                            $parseStr ='';
                        }
                    } elseif (strtolower ( $type ) == 'lastpage') { // 最后一页
                        $text = empty($text)?'最后一页':$text;
                        if ($nowCoolPage == $coolPages) {
                        } else {
                            $nextRow = $nowPage + $rollPage;
                            if($nextRow > $totalPages) {

                            } else {
                            	//OOO to add 下五也处理
                                //$parseStr = "<a href='".$url."_".$nextRow.'.'.$prex."'>下".$rollPage."页</a>&nbsp;";
                            }
                        }
                        if ($nowPage == $totalPages) {
                            //$parseStr .= $text.'&nbsp;';
                        	$parseStr .= '';
                        } else {
                            $parseStr .= " <span class=\"last\"><a href='".$url."_".$totalPages.'.'.$prex."'>".$text."&nbsp;</a></span>";
                        }
                    } elseif(strtolower($type) == 'currentpageindex') { //当前页
                        $text = empty($text)?'当前页：':$text;
                        $parseStr = $text.$nowPage;
                    } elseif (strtolower ( $type ) == 'jumppage') { // 跳转到
                        $text = empty($text)?'跳转到':$text;
                        $parseStr = $text . ":<select onChange='javascript:window.location.href=(this.options[this.selectedIndex].value);'>";
                        for($i = 1; $i <= $totalPages; $i ++) {
                            if ($i == $nowPage) {
                                $parseStr .= "<a>" . $i . "</a>";
                                $parseStr .= "<option value='".$url."_".$i.'.'.$prex."' selected='selected'>".$i."</option>";
                            } else {
                                if($i == 1) {
                                    $parseStr .= "<option value='".$url.'.'.$prex."'>".$i."</option>";
                                } else {
                                    $parseStr .= "<option value='".$url."_".$i.'.'.$prex."'>".$i."</option>";
                                }
                            }
                        }
                        $parseStr .= '</select>';
                    } elseif (strtolower($type) === 'totalpagenum') {
                        $parseStr = $totalPages;
                    }
                }
                $content = str_replace($match, $parseStr, $content);
            }
        }
        return $content;
    }
}